package com.visikard.ui.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.visikard.application.UIMessage;
import com.visikard.application.VKRegApplication;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.GetAudienceHistoryTask;
import com.visikard.item.ItemAudienceKard;
import com.visikard.ui.R;
import com.visikard.ui.activity.CouponsRTUIMainActivity;
import com.visikard.ui.adapter.AudienceKardAdapter;

public class ListAudienceFragment extends BaseFragment {

	public static final String MSG_SEND_IDCAMPAGINSPECIALS = "SendIdCampaginSpecails";

	private ListView listView;
	private ImageView btnBack, btnSearch;

	private long idCampaignSpecail = -1;
	private int pageIndex = 1;
	private boolean isNoLoadMore = false;
	private boolean isLoadingMore = false;

	private AudienceKardAdapter audienceAdapter;
	private ArrayList<ItemAudienceKard> listAudience = new ArrayList<ItemAudienceKard>();

	private BaseTask mAsyncTask;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.list_audience_fragment, container, false);
		Bundle bundle = getArguments();
		if (bundle != null) {
			idCampaignSpecail = bundle.getLong(MSG_SEND_IDCAMPAGINSPECIALS, -1);
		}
		innitControl(view);
		if (idCampaignSpecail > 0)
			getListAudience();
		return view;
	}

	private void innitControl(View view) {
		listView = (ListView) view.findViewById(R.id.list_audience_kard);
		btnBack = (ImageView) view.findViewById(R.id.back_btn_menu);
		btnSearch = (ImageView) view.findViewById(R.id.search_btn_menu);

		btnBack.setOnClickListener(onclick);
		btnSearch.setOnClickListener(onclick);

		listView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				// For load more
				if (totalItemCount > 2 && firstVisibleItem + visibleItemCount >= totalItemCount && !isLoadingMore
						&& !isNoLoadMore) {
					pageIndex++;
					isLoadingMore = true;
					getMoreListAudience();
					// load more
				}
			}
		});
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	OnClickListener onclick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.back_btn_menu:
					((CouponsRTUIMainActivity) getActivity()).onBackPressed();
					break;
				case R.id.search_btn_menu:

					break;
				default:
					break;
			}

		}
	};

	private void getListAudience() {
		mAsyncTask = new GetAudienceHistoryTask(mHandler);
		mAsyncTask.execute(VKRegApplication.uid, idCampaignSpecail, pageIndex, VKRegApplication.mPageSize);
	}

	private void getMoreListAudience() {
		mAsyncTask = new GetAudienceHistoryTask(mHandler);
		mAsyncTask.execute(VKRegApplication.uid, idCampaignSpecail, pageIndex, VKRegApplication.mPageSize);
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean handleMessage(Message msg) {
		if (getActivity() == null)
			return false;
		switch (msg.what) {
			case UIMessage.MSG_GET_LIST_SUCCESS:
				ArrayList<ItemAudienceKard> listResults = (ArrayList<ItemAudienceKard>) msg.obj;
				if (listResults != null && listResults.size() < VKRegApplication.mPageSize)
					isNoLoadMore = true;
				if (listResults != null && listResults.size() > 0) {
					listAudience.addAll(listResults);
				}
				if (listAudience != null && listAudience.size() > 0) {
					listView.setVisibility(View.VISIBLE);
					if (audienceAdapter == null) {
						audienceAdapter = new AudienceKardAdapter(getActivity(), listAudience, mHandler);
						listView.setAdapter(audienceAdapter);
					}
					else
						audienceAdapter.notifyDataSetChanged();
				}
				else
					listView.setVisibility(View.GONE);

				isLoadingMore = false;
				return true;
		}
		return super.handleMessage(msg);
	}
}
